//
//  SNSManager.m
//  KiklinkApp
//
//  Created by LF on 14-9-25.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "SNSManager.h"
#import "UMSocial.h"
#import "UMSocialQQHandler.h"
#import "UMSocialSinaHandler.h"
#import "UMSocialDataService.h"
#import "UMSocialWechatHandler.h"

NSString* const UM_APP_KEY = @"507fcab25270157b37000010";

NSString* const QQ_APP_KEY = @"7r7c165MwMisP4ET";
NSString* const QQ_APP_ID = @"1103426446";
NSString* const QQ_URL_SCHEME = @"QQ41C4F38E";

NSString* const WX_APP_ID = @"wxc81b46e85604df8b";
NSString* const WX_APP_SECRET = @"b371ff4935c0d7016abc7ea744effd2c";

@implementation SNSManager

+ (void) initManager
{
    KLLogStatic(self, @"initSNSManager");
    
    static SNSManager* sharedLoginManagerInstance = nil;
    if (sharedLoginManagerInstance == nil) {
        [UMSocialData setAppKey:UM_APP_KEY];
        sharedLoginManagerInstance = [[self alloc] init];
    }
    
    [UMSocialQQHandler setQQWithAppId:QQ_APP_ID appKey:QQ_APP_KEY url:@"http://www.kiklink.com"];
    
    [UMSocialWechatHandler setWXAppId:WX_APP_ID appSecret:WX_APP_SECRET url:@"http://www.kiklink.com"];
    
    [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
}

+ (BOOL) isLoggedInWithPlatform: (NSString *) platform
{
    return [UMSocialAccountManager isOauthWithPlatform:platform];
}

+ (BOOL) isLoggedInAndTokenNotExpiredWithPlatform: (NSString *) platform
{
    return [UMSocialAccountManager isOauthAndTokenNotExpired:platform];
}


+ (void) openSNSLoginUIWithPlatform: (NSString*) platform ViewController: (UIViewController*) sourceViewController callback:(Callback)callback
{
    KLLogStatic(self, @"openSNSLoginUI");
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:platform];
    
    snsPlatform.loginClickHandler(sourceViewController,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response)
                                  {
                                      KLLogVerboseStatic(self, response);
                                      if(response.responseCode == UMSResponseCodeSuccess)
                                      {
                                          callback(SUCCESS, @"Success", response);
                                      }
                                      else{
                                          callback(FAILURE, response.message, response);
                                      }
                                  });
}

+ (void) userInfoWithCallback: (Callback)callback
{
    KLLogStatic(self, @"getUserInfo");
    
    [[UMSocialDataService defaultDataService] requestSocialAccountWithCompletion:^(UMSocialResponseEntity *accountResponse){
        
        KLLogVerboseStatic(self, accountResponse);
        
        int statusCode = 0;
        if(accountResponse.responseCode == UMSResponseCodeSuccess)
        {
            statusCode = SUCCESS;
        }
        else
        {
            statusCode = FAILURE;
        }
        NSString* word = accountResponse.message;
        
        callback(statusCode, word, accountResponse);
    }];
}

+ (void) userNameWithCallback: (Callback) callback
{
    KLLogStatic(self, @"getUserName");
    
    [SNSManager userInfoWithCallback:^(int stateCode, NSString *word, id content) {
        if(stateCode > 0)
        {
            UMSocialResponseEntity* accountResponse = content;
            NSString* username = [[[accountResponse.data objectForKey: @"accounts"] objectForKey: UMShareToSina]objectForKey: @"username"];
            callback(stateCode, username, nil);
        }
        else
        {
            callback(stateCode, word, content);
        }
    }];
    
    //    if ([platformName isEqualToString:UMShareToSina]) {
    //    }
    
    
    //            NSLog(@"SinaWeibo's user name is %@", [[[accountResponse.data objectForKey: @"accounts"] objectForKey: UMShareToSina] objectForKey: @"username"]);
    
    //这里可以获取到腾讯微博openid,Qzone的token等
    /*
     else if ([platformName isEqualToString:UMShareToTencent]) {
     [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToTencent completion:^(UMSocialResponseEntity *respose){
     NSLog(@"get openid  response is %@",respose);
     }];
     }
     */
}

+ (void) userTokenWithCallback: (Callback) callback
{
    KLLogStatic(self, @"getUserToken");
    
    [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina completion:^(UMSocialResponseEntity *response){
        
        KLLogVerboseStatic(self, response);
        
        if(response.responseCode == UMSResponseCodeSuccess)
        {
            NSString *token = [response.data objectForKey:@"access_token"];
            callback(SUCCESS, token, response);
        }
        else
        {
            callback(FAILURE, response.message, response);
        }
    }];
}

+ (void) openSNSShareUIWithSharedText:(NSString*)text shareImage:(UIImage*)image sourceViewController:(UIViewController*)sourceViewController
{
    KLLogStatic(self, @"openSNSShareUI");
    
    [UMSocialSnsService presentSnsIconSheetView:sourceViewController appKey:UM_APP_KEY shareText:text shareImage:image shareToSnsNames:[NSArray arrayWithObjects:UMShareToSms, UMShareToSina, UMShareToQQ, UMShareToWechatSession, UMShareToWechatTimeline, UMShareToWechatFavorite, nil] delegate:nil];
}
@end
